//
//  JKPopoverView.swift
//  afanti
//
//  Created by albert on 16/4/16.
//  Copyright © 2016年 albert. All rights reserved.
//

import UIKit

protocol JKPopoverViewDataSource: NSObjectProtocol {
    /** 返回多少行 */
    func numberOfRowsInMainTableViewpopoverView(popoverView: JKPopoverView) -> Int
    
    /** 设置每行的标题 */
    func popoverView(popoverView: JKPopoverView, titleInrow: NSInteger) -> String
    
    /** 设置每行的普通图片名称 */
    func popoverView(popoverView: JKPopoverView, imageNameInrow: NSInteger) -> String?
    
    /** 设置每行的高亮图片名称 */
    func popoverView(popoverView: JKPopoverView, hightImageNameInrow: NSInteger) -> String?
}

protocol JKPopoverViewDelegate: NSObjectProtocol {
    /** 选中了某一行 */
    func popoverView(popoverView: JKPopoverView, didSelectIndex: NSInteger)
    /** 设置每行的高度 */
    func popoverView(popoverView: JKPopoverView, heightForRowAtIndexPath: NSIndexPath) -> CGFloat
}

class JKPopoverView: UIView {
     
    //保存数据源和代理
    weak var dataSource: JKPopoverViewDataSource?
    weak var delegate: JKPopoverViewDelegate?
    
    //MARK: - 懒加载
    lazy var tableView: UITableView = {
        
        let tableView = UITableView()
        tableView.dataSource = self
        tableView.delegate = self
        tableView.showsVerticalScrollIndicator = false
        tableView.separatorStyle = UITableViewCellSeparatorStyle.None
        tableView.backgroundColor = UIColor.clearColor()
        
        let bgView = UIImageView(frame: tableView.bounds)
        bgView.image = UIImage(named: "mainCellBackground")
        tableView.backgroundView = bgView
        self.addSubview(tableView)
        return tableView;
    }()
    
    
    //MARK: - 初始化
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //添加点击事件
        let tap = UITapGestureRecognizer(target: self, action: "remove")
        tap.delegate = self
        self.addGestureRecognizer(tap)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        if (popFrame!.size.width != 0 && popFrame!.size.height != 0) {
            self.tableView.frame = popFrame!
        }else{
            self.tableView.bounds = CGRectMake(0, 0, JKScreenW * 0.6, JKScreenH * 0.6);
            self.tableView.center = self.center;
        }
    }
    
    //点击空白移除
    func remove(){
        self.removeFromSuperview()
    }
    
    //设置tableView的frame
    var popFrame: CGRect? {
        didSet{
            self.layoutIfNeeded()
        }
    }
}

extension JKPopoverView: UITableViewDataSource, UITableViewDelegate, UIGestureRecognizerDelegate{
    
    //MARK: - UITableViewDataSource
    /** 返回多少行 */
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int{
        return (dataSource?.numberOfRowsInMainTableViewpopoverView(self))!
    }
    
    /** 设置cell */
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        //创建cell
        let reuseId = "pop";
        var cell = tableView.dequeueReusableCellWithIdentifier(reuseId)
        if (cell == nil) {
            cell = JKPopoverCell(style: UITableViewCellStyle.Default, reuseIdentifier: reuseId)
            cell?.textLabel?.font = UIFont.systemFontOfSize(13)
            cell?.backgroundColor = UIColor.clearColor()
        }
        
        //设置cell文字
        cell!.textLabel!.text = dataSource?.popoverView(self, titleInrow: indexPath.row)
        
        //cell普通状态图片
        if ((self.dataSource?.respondsToSelector("popoverView:imageNameInrow:")) != nil){
            cell!.imageView!.image = UIImage(named: (dataSource?.popoverView(self, imageNameInrow: indexPath.row))!)
        }
        
        //cell高亮状态图片
        if ((self.dataSource?.respondsToSelector("popoverView:imageNameInrow:")) != nil){
            let image = (dataSource?.popoverView(self, hightImageNameInrow: indexPath.row))
            
            if image != nil {
                cell!.imageView!.image = UIImage(named: image!)
            }
        }
        
        return cell!
    }
    
    //MARK: - UITableViewDelegate
    /** 选中了某一行 */
    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        if ((self.delegate?.respondsToSelector("popoverView:didSelectIndex:")) != nil) {
            delegate?.popoverView(self, didSelectIndex: indexPath.row)
        }
    }
    
    /** 设置每行的高度 */
    func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        
        if ((delegate?.respondsToSelector("popoverView:heightForRowAtIndexPath:")) != nil){
            return delegate!.popoverView(self, heightForRowAtIndexPath: indexPath)
        }else{
            return 44;
        }
    }
    
    //MARK: - UIGestureRecognizerDelegate
    func gestureRecognizer(gestureRecognizer: UIGestureRecognizer, shouldReceiveTouch touch: UITouch) -> Bool {
        
        let str = "\(touch.view!)"

        if  str.containsString("UITableViewCellContentView") {
            return false
        }else{
            return true
        }
    }
}